package jrain.flow.engine.bean;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import jrain.flow.engine.uitls.FlowConst;

public class FlowDef {

	private long fdId;

	private long fdCode;

	private Map<String, Object> attrMap = new HashMap<>();

	private List<VarDef> vars = new ArrayList<>();

	private List<NodeDef> nodes = new ArrayList<>();

	private List<ConnDef> conns = new ArrayList<>();
	

	public FlowDef() {

	}

	public long getFdId() {
		return fdId;
	}

	public void setFdId(long fdId) {
		this.fdId = fdId;
	}

	public long getFdCode() {
		return fdCode;
	}

	public void setFdCode(long fdCode) {
		this.fdCode = fdCode;
	}

	// =========================================================================
	public Map<String, Object> getAttrMap() {
		return attrMap;
	}

	public void setAttrMap(Map<String, Object> attrMap) {
		this.attrMap = attrMap;
	}

	public void putAttr(String key, Object value) {
		this.attrMap.put(key, value);
	}

	public Object getAttr(String key) {
		return this.attrMap.get(key);
	}

	public Object removeAttr(String key) {
		return this.attrMap.remove(key);
	}

	public void cleanAttr() {
		this.attrMap.clear();
	}
	// =========================================================================

	public List<VarDef> getVars() {
		return vars;
	}

	public void setVars(List<VarDef> vars) {
		for (VarDef varDef : vars) {
			addVar(varDef);
		}
	}

	public void addVar(VarDef varDef) {
		varDef.setFlowDef(this);
		this.vars.add(varDef);
	}

	public void removeVar(String varKey) {
		if (vars != null && varKey != null) {
			for (Iterator<VarDef> iterator = vars.iterator(); iterator.hasNext();) {
				VarDef varDef = (VarDef) iterator.next();
				if (varKey.equals(varDef.getKey())) {
					iterator.remove();
				}
			}
		}
	}

	public VarDef getVar(String varKey) {
		for (Iterator<VarDef> iterator = vars.iterator(); iterator.hasNext();) {
			VarDef varDef = (VarDef) iterator.next();
			if (varKey.equals(varDef.getKey())) {
				return varDef;
			}
		}
		return null;
	}
	// =========================================================================

	public List<NodeDef> getNodes() {
		return nodes;
	}

	public void setNodes(List<NodeDef> nodes) {
		for (NodeDef nodeDef : nodes) {
			addNode(nodeDef);
		}
	}

	public void addNode(NodeDef nodeDef) {
		nodeDef.setFlowDef(this);
		this.nodes.add(nodeDef);
	}

	public void removeNode(String nodeCode) {
		if (nodes != null && nodeCode != null) {
			for (Iterator<NodeDef> iterator = nodes.iterator(); iterator.hasNext();) {
				NodeDef nodeDef = (NodeDef) iterator.next();
				if (nodeCode.equals(nodeDef.getCode())) {
					iterator.remove();
				}
			}
		}
	}

	public NodeDef getNode(String nodeCode) {
		for (Iterator<NodeDef> iterator = nodes.iterator(); iterator.hasNext();) {
			NodeDef nodeDef = (NodeDef) iterator.next();
			if (nodeCode.equals(nodeDef.getCode())) {
				return nodeDef;
			}
		}
		return null;
	}

	// =========================================================================
	public List<ConnDef> getConns() {
		return conns;
	}

	public void setConns(List<ConnDef> conns) {
		this.conns = conns;
	}

	public void connDef(ConnDef connDef) {
		connDef.setFlowDef(this);
		this.conns.add(connDef);
	}

	public void removeConnDef(String sid, String tid) {
		if (conns != null && sid != null && tid != null) {
			for (Iterator<ConnDef> iterator = conns.iterator(); iterator.hasNext();) {
				ConnDef connDef = (ConnDef) iterator.next();
				if (sid.equals(connDef.getSid()) && tid.equals(connDef.getTid())) {
					iterator.remove();
				}
			}
		}
	}

	public ConnDef getConnDef(String sid, String tid) {
		if (conns != null && sid != null && tid != null) {
			for (Iterator<ConnDef> iterator = conns.iterator(); iterator.hasNext();) {
				ConnDef connDef = (ConnDef) iterator.next();
				if (sid.equals(connDef.getSid()) && tid.equals(connDef.getTid())) {
					return connDef;
				}
			}
		}
		return null;
	}

	public List<ConnDef> getConnDefBySid(String sid) {
		List<ConnDef> rsList = new ArrayList<>();
		if (conns != null && sid != null) {
			for (Iterator<ConnDef> iterator = conns.iterator(); iterator.hasNext();) {
				ConnDef connDef = (ConnDef) iterator.next();
				if (sid.equals(connDef.getSid())) {
					rsList.add(connDef);
				}
			}
		}
		return rsList;
	}

	public List<ConnDef> getConnDefByTid(String tid) {
		List<ConnDef> rsList = new ArrayList<>();
		if (conns != null && tid != null) {
			for (Iterator<ConnDef> iterator = conns.iterator(); iterator.hasNext();) {
				ConnDef connDef = (ConnDef) iterator.next();
				if (tid.equals(connDef.getTid())) {
					rsList.add(connDef);
				}
			}
		}
		return rsList;
	}
	
	public NodeDef getStartNode(){
		for (Iterator<NodeDef> iterator = nodes.iterator(); iterator.hasNext();) {
			NodeDef nodeDef = (NodeDef) iterator.next();
			boolean b =FlowConst.NODE_TYPE_START.equals(nodeDef.getNodeType());
			if(b){
				return nodeDef;
			}
		 
		}
		return null;
	}
}
